<?php

namespace app\common\validate\crm;

use think\Validate;
use app\common\service\crm\ContractService;

/**
 * 功能介绍:费用输入验证表单
 * @author lvyubing
 * @date 2022/7/29
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class Contract extends Validate
{
    protected $rule = [
        'name' => 'require',
        'num' => 'require|dbunique',
        'money' => 'require',
        'order_date' => 'require',
        'start_time' => 'require',
        'end_time' => 'require',
        'company_id' => 'require',
        'order_user_id' => 'require',
        'customer_id' => 'require',
        'contacts_id' => 'require',
    ];
    protected $message = [
        'name.require' => '请输入合同名称',
        'num.require' => '请输入合同编号',
        'money.require' => '请输入金额',
        'order_date.require' => '请选择下单时间',
        'start_time.require' => '请选择开始时间',
        'end_time.require' => '请选择结束时间',
        'company_id.require' => '请选择公司',
        'order_user_id.require' => '请选择签单人',
        'customer_id.require' => '请选择客户',
        'contacts_id.require' => '请选择客户联系人',
    ];

    protected $scene = [
        'edit' => ['name', 'money','start_time','end_time'],
        'add' => ['name', 'num', 'money', 'order_date', 'start_time', 'end_time', 'company_id', 'order_user_id', 'customer_id', 'contacts_id'],
    ];

    protected function dbunique($value)
    {
        $where = [];
        $where[] = ["num", "=", $value];
        return (new ContractService)->getTotal($where) == 0 ? true : "当前合同编码冲突,请重新生成!";
    }
}